import axios from "axios";
import {watch} from "vue";
import {useStore} from "vuex";

function ApiObject(){
    const apiClient = axios.create({
        baseURL: "http://localhost:9000/sr/api/",
        headers: {
            Accept: "application/json"
        },
    })
    if (localStorage.getItem('token')){
        apiClient.defaults.headers.common['Authorization'] = `Bearer ${localStorage.getItem('token')}`
    }
    return apiClient;
}

const apiClient = new ApiObject()
export default {
    setToken(token){
        apiClient.defaults.headers.common['Authorization'] = `Bearer ${token}`
    },
    getInstance(){
        return apiClient;
    },
    get(url){
        return apiClient.get(url)
    },
    post(url, data){
        return apiClient.post(url, data)
    },
    postWithParams(url, params){
        return apiClient.post(url, {params})
    },
    put(url, data){
        return apiClient.put(url, data)
    },
    delete(url){
        return apiClient.delete(url)
    },
    getMenuList(){
        return apiClient.get("user/menu")
    },
    getWithQuery(url, queryData){
        return apiClient.get(url, {headers:{'X-Query-Data': encodeURIComponent(JSON.stringify(queryData))}})
    },
    getPage(url, params, queryData){
        return apiClient.get(url, {params:params,headers:{'X-Query-Data': encodeURIComponent(JSON.stringify(queryData))}} )
    },
    getPageNoQuery(url, params){
        return apiClient.get(url, {params:params} )
    },
    login(param){
        return apiClient.post("auth/login", param)
    },
    register(param){
        return apiClient.post("auth/register", param)
    },
    logout(token){
        return apiClient.post("auth/logout", {token})
    }
}